<template>
	<view v-if="visible" class="custom-modal-mask" @click="handleMaskClick">
		<view class="custom-modal-container" @click.stop>
			<view class="custom-modal-header">
				<text class="modal-title">{{ title }}</text>
			</view>
			<view class="custom-modal-body">
				<!-- 使用 rich-text 来解析换行符 -->
				<rich-text :nodes="formattedContent"></rich-text>
			</view>
			<view class="custom-modal-footer">
				<button class="modal-button" @click="handleConfirm">{{ confirmText }}</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			visible: {
				type: Boolean,
				default: false
			},
			title: {
				type: String,
				default: '提示'
			},
			content: {
				type: String,
				default: ''
			},
			confirmText: {
				type: String,
				default: '确定'
			}
		},
		computed: {
			formattedContent() {
				// 将 \n 替换为 <br/> 以便 rich-text 解析
				return this.content.replace(/\n/g, '<br/>');
			}
		},
		methods: {
			handleConfirm() {
				this.$emit('confirm');
			},
			handleMaskClick() {
				this.$emit('close');
			}
		}
	}
</script>

<style scoped>
	.custom-modal-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}

	.custom-modal-container {
		width: 80%;
		max-width: 300px;
		background-color: #ffffff;
		border-radius: 12px;
		overflow: hidden;
	}

	.custom-modal-header {
		padding-top: 20px;
		padding-bottom: 10px;
		align-items: center;
	}

	.modal-title {
		font-size: 18px;
		font-weight: 600;
		color: #333;
		text-align: center;
	}

	.custom-modal-body {
		padding: 10px 20px 20px;
		align-items: center;
	}

	/* rich-text 默认不是 flex 布局，需要额外处理 */
	/deep/ .rich-text {
		font-size: 16px;
		color: #666;
		text-align: center;
		line-height: 1.5;
	}

	.custom-modal-footer {
		border-top: 1px solid #f0f0f0;
		flex-direction: row;
	}

	.modal-button {
		flex: 1;
		height: 50px;
		line-height: 50px;
		border: none;
		background-color: transparent;
		color: #409eff;
		font-size: 17px;
		border-radius: 0;
	}

	.modal-button::after {
		border: none;
	}
</style>
